package com.zxb.core.common;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.*;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

import com.zxb.core.util.html.EscapeUtil;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import com.zxb.core.util.TokenUtil;

/**
 * @author Zxb
 */
@Component
public class Req {
    public static HttpServletRequest getRequest() {
        //原理：spring框架把request相关数据放到了线程本地变量
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        if (requestAttributes == null) {
            return null;
        }
        return (HttpServletRequest) requestAttributes
                .resolveReference(RequestAttributes.REFERENCE_REQUEST);
    }

    @SuppressWarnings("AlibabaLowerCamelCaseVariableNaming")
    public static String getURI() {
        HttpServletRequest request = getRequest();
        if (request == null) {
            return null;
        }
        return request.getRequestURI();
    }

    public static String getMethod() {
        HttpServletRequest request = getRequest();
        if (request == null) {
            return null;
        }
        return request.getMethod();
    }

    public static String getIp() {
        String unknown = "unknown";
        int ipLength = 15;
        HttpServletRequest request = getRequest();
        if (request == null) {
            return null;
        }
        String ip = request.getHeader("X-Forwarded-For");
        if (ip == null || ip.length() == 0 || unknown.equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || unknown.equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || unknown.equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_CLIENT_IP");
        }
        if (ip == null || ip.length() == 0 || unknown.equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_X_FORWARDED_FOR");
        }
        if (ip == null || ip.length() == 0 || unknown.equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }

        if (ip.length() > ipLength) {
            String[] ipArr = ip.split(",");
            for (String str : ipArr) {
                if (!"unknown".equalsIgnoreCase(str)) {
                    ip = str;
                    break;
                }
            }
        }
        return ip;
    }

    public static Map<String, String[]> getParameterMap() {
        HttpServletRequest request = getRequest();
        if (request == null) {
            return null;
        }
        return request.getParameterMap();
    }

    @SuppressWarnings("unused")
    public static String getCookie(String name) {
        HttpServletRequest req = getRequest();
        if (req == null) {
            return null;
        }
        Cookie[] cookies = req.getCookies();
        if (cookies != null) {
            // 遍历数组
            for (Cookie cookie : cookies) {
                if (cookie.getName().equals(name)) {
                    // 取出cookie的值
                    return cookie.getValue();
                }
            }
        }
        return null;
    }

    public static String getHeader(String name) {
        HttpServletRequest req = getRequest();
        if (req == null) {
            return null;
        }
        return req.getHeader(name);
    }

    public static String getToken() {
        HttpServletRequest req = getRequest();
        if (req == null) {
            return null;
        }
        return req.getHeader(C.TOKEN_KEY);
    }

    public static User getCurrUser() {
        String token = getToken();
        if (token == null) {
            return null;
        }
        return TokenUtil.validateToken(token);
    }

    public static String getString(String name) {
        HttpServletRequest req = getRequest();
        if (req == null) {
            return null;
        }
        String value = req.getParameter(name);
        A.isNotNull(value, name);
        //xss过滤
        value = EscapeUtil.clean(value).trim();
        try {
            //对参数进行解码
            value = URLDecoder.decode(value, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return value;
    }
}
